package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import DBTasks.AddPet;
import DBTasks.BreedList;

@SuppressWarnings("serial")
public class AddPetPanel extends JPanel implements ActionListener{

	private JButton submitButton;
	private JTextField nameInput;
	private JComboBox typeList, breedList, genderList, ageList;
	private String shelterName;
	private MainFrame frame;
	private JComboBox[] boxes;
	private JButton returnButton;

	public AddPetPanel(MainFrame mainFrame) {
	
		this.setSize(550,600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		shelterName = (String) frame.getLogin().getShelterList().getSelectedItem();
		JLabel addLabel = new JLabel("Add New Pet for " + shelterName);
		addLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		submitButton = new JButton("Submit Pet");
		submitButton.addActionListener(this);
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		JLabel type = new JLabel("Type:");
		JLabel breed = new JLabel("Breed:");
		JLabel gender = new JLabel("Gender:");
		JLabel age = new JLabel("Age:");
		JLabel name = new JLabel("Pet Name:");
		
		nameInput = new JTextField(20);
		
		String[] types = {"Dog", "Cat"};
		typeList = new JComboBox(types);
		typeList.setSelectedIndex(0);
		typeList.addActionListener(this);
		
		boxes = BreedList.listBreeds();
	    
		breedList = new JComboBox();
		try {
			setComboBox("Dog");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		breedList.setSelectedIndex(0);
		
		String[] genders = {"M", "F"};
		genderList = new JComboBox(genders);
		genderList.setSelectedIndex(0);
		
		String[] ages = {"Baby", "Young", "Middle Age", "Old"};
		ageList = new JComboBox(ages);
		ageList.setSelectedIndex(0);
		
		GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addComponent(addLabel)
				.addGroup(gl.createSequentialGroup()
						.addGap(100)
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(type)
								.addComponent(breed)
								.addComponent(gender)
								.addComponent(age)
								.addComponent(name))
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(typeList)
								.addComponent(breedList)
								.addComponent(genderList)
								.addComponent(ageList)
								.addComponent(nameInput))
						.addGap(100))
				.addGroup(gl.createSequentialGroup()
						.addComponent(submitButton)
						.addGap(20)
						.addComponent(returnButton)));
		
		gl.linkSize(SwingConstants.VERTICAL, nameInput, typeList, breedList, genderList, ageList);
		
		gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addComponent(addLabel)
			    .addGap(15)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
			        .addGroup(gl.createSequentialGroup()
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
					        .addComponent(type)
					        .addComponent(typeList))
					    .addGap(20)
			        	.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
					        .addComponent(breed)
					        .addComponent(breedList))
					    .addGap(20)
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
			                .addComponent(gender)
			                .addComponent(genderList))
			            .addGap(20)
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
			                .addComponent(age)
			                .addComponent(ageList))
			            .addGap(20)
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
							.addComponent(name)
						    .addComponent(nameInput))
						.addGap(20))
			     )
			     .addGap(15)
				 .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
						 .addComponent(submitButton)
						 .addComponent(returnButton))
				 .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		gl.setAutoCreateGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource().equals(submitButton)) {
			
			boolean check = true;
			if(breedList.getModel() == boxes[1].getModel()) {
				check = false;
			}
			if(AddPet.addPet(shelterName, check, (String) breedList.getSelectedItem(), (String) genderList.getSelectedItem(), 
					  nameInput.getText(), (String) ageList.getSelectedItem())) {
				frame.setEmployeePanels();
				frame.setPanel(frame.getHomepage());
				System.out.println(nameInput.getText() + " has been accepted to " + shelterName);
			}
			else {
				System.out.println("Invalid Entry. Try Again.");
			}
			
        }
		
		else if(e.getSource().equals(returnButton)) {
			frame.setAddPet(new AddPetPanel(frame));
			frame.setPanel(frame.getHomepage());
		}
		
		else if(e.getSource().equals(typeList)) {
			JComboBox cb = (JComboBox)e.getSource();
	        String petType = (String)cb.getSelectedItem();
	        try {
				setComboBox(petType);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}
	
	public void setComboBox(String type) throws SQLException {
		
		if(type == "Dog") {
			breedList.setModel(boxes[0].getModel());
		}
		
		if(type == "Cat") {
			breedList.setModel(boxes[1].getModel());
		}
		
	}

}
